package com.example.x.dnuifood_929dx.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.bean.ShopBean;
import com.example.x.dnuifood_929dx.controller.FoodListActivity;
import com.example.x.dnuifood_929dx.model.Constants;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by X on 2018/11/15.
 */

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private List<ShopBean> mDataList;
    private LayoutInflater mLayoutInflater;
    private  OnRecyclerViewItemClickListener mOnItemClickListener=null;
    public ShopAdapter (Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener!=null){
            //注意这里使用 getTag 方法获取数据
            mOnItemClickListener.onItemClick(v,v.getTag());
        }
    }

    public void setList(List<ShopBean> list) {
        this.mDataList = list;
    }

    public interface OnRecyclerViewItemClickListener{
        void onItemClick(View view,Object data);
    }
    @Override

    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {                                      //调用shop_itme  加载样式
        View v = mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item, viewGroup,
                false);
        v.setOnClickListener(this);    // 在 onCreateViewHolder()中 中 为每个 个 item  添加 点 击事 件。
        return new ViewHolder(v);

    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {                       //数据填充 绑定 position（控制条目位置） 对应的数据项
        final ShopBean entity =mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());

        Picasso.with(mContext).load(Constants.BASE_URL+entity.getPic()).into(viewHolder.image);

        // 在 onBindViewHolder()方法中设置和 item 相关的数据
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,FoodListActivity.class);
                intent.putExtra("shop_id",entity.getShop_id().toString());

                intent.putExtra("shop_name",entity.getShopname().toString().trim());
                intent.putExtra("phonenum", entity.getPhonenum());
                mContext.startActivity(intent);
            }
        });


    }
    //暴露给外面的调用者:定义一个设置 Listener 的方法。
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override



    public int getItemCount() {
        if(mDataList!=null)
            return mDataList.size();
        else
            return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {                                          //继承 RecyclerView.ViewHolder 获取 View 参数中各控件
        RatingBar res_bar;
        TextView res_name, res_address;
        ImageView image;
        public ViewHolder(View view) {
            super(view);
            res_name = (TextView) view.findViewById(R.id.res_name);
            res_bar = (RatingBar) view.findViewById(R.id.ratingBar1);
            image = (ImageView) view.findViewById(R.id.res_image);
            res_address = (TextView) view.findViewById(R.id.res_address);
        } }

}
